--
-- Author: songjianbang
-- Date: 2018-07-04 11:41:16
--
-- DailyRewardView
--
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.DailyTaskReward

local rewardNum = 3 -- 奖励个数

function ClassRef:init()
    self.title = self._ui["Panel/tlt"]
    self.title:setString(L("lua_code_text_152"))

    self._ui["Panel/tips_1"]:hide()
    self._ui["Panel/tips_img"]:hide()

	self._isLock = false
	self.changeItemName = true
end

function ClassRef:showData( itemLists)
	self:initView()
	self:refreshReward(itemLists)
	self:setNodeEventEnabled(true)
end

function ClassRef:initView()
	local rootPanel = self._ui["Panel"]
	local lockMask = rootPanel:findChild("icon/mask")
	lockMask:setVisible(self._isLock)
	display.nodeRemoveForNotHit(rootPanel, function()
		self:closeView()
    end)
    
	local btnOk = rootPanel:getChildByName("Button_ok")
	btnOk:getChildByName( "name" ):setString( self._isLock and L("text_10200017") or L("tips_2000235") )
	btnOk:addClickEventListener(function(sender)
		self:closeView()
	end)
end

-- 刷新奖励部分
function ClassRef:refreshReward( itemLists )
    local rootPanel = self._ui["Panel"]
    local nodes = {}

	for i = 1, rewardNum do
		local iconRoot = rootPanel:getChildByName( "icon_" .. i )
		local uiItem = IconRender.create( iconRoot )
		uiItem:setTipEnable( true )
		if self.changeItemName then
			-- 暂时不需要显示名字，ui上面没有name
			-- uiItem:getChildByName("name"):setString("任务奖励")
		end
		local dataTemp = itemLists[i]

        if dataTemp then
            uiItem:setData( dataTemp )
            nodes[#nodes + 1] = uiItem
        else
            uiItem:hide()
        end
    end
	
	local function centerNodes(p, nodes, gap)
		if p == nil then return end

		local panelWidth = p:getContentSize().width
		local totalWidth = 0
		if nodes and #nodes > 0 then
			for i, v in ipairs(nodes) do
				totalWidth = totalWidth + v:getContentSize().width + gap
			end

			local offx = (panelWidth - totalWidth + gap) * 0.5

			for i, v in ipairs(nodes) do
				v:setPositionX(offx + v:getAnchorPoint().x * v:getContentSize().width)
				offx = offx + v:getContentSize().width + gap
			end
		end
	end
	centerNodes(rootPanel, nodes, 10)
    -- display.autoAdjustNodesPosX( rootPanel:getContentSize().width, nodes, #nodes )
end

function ClassRef:refreshConfig(data)
	if data.title then
		self.title:setString(L(""))
	end
	self.changeItemName = data.changeName
end

function ClassRef.showRewardView(data, closeCb)
	local dropLists = UD:parseDropStrToBagItem(data.drops)

    local view = ClassRef.new()
	view:onCloseView(function()
		if closeCb then
			closeCb()
		end
        UIHandler:removePopupUiByKey(view:getViewName())
    end)
    UIHandler:addPopupUi(view, view:getViewName())
	view:show()
	view.changeItemName = false
	if data.title then
		view.title:setString(data.title)
	end
	
	view:showData(dropLists)
	if data.buttonName then
		view._root:findChild("Panel/Button_ok/name"):setString(data.buttonName)
	end
end

return ClassRef
